# @FileName  :  auth_data.py
# @Time      :  2025/8/26 09:25
# @Author    :  Ayanami
# @info      :
import asyncio
from tortoise import Tortoise
from app.core.config import TORTOISE_ORM
from app.models.models import Role, Permission


async def init_auth_data():
    """初始化认证基础数据"""
    await Tortoise.init(config=TORTOISE_ORM)

    # 创建基础角色
    roles = [
        {"name": "机构管理员", "code": "ADMIN", "description": "机构管理员", "is_system": True},
        {"name": "教师", "code": "TEACHER", "description": "教师角色", "is_system": True},
        {"name": "学员", "code": "STUDENT", "description": "学员角色", "is_system": True},
    ]

    for role_data in roles:
        role, created = await Role.get_or_create(code=role_data['code'], defaults=role_data)
        if created:
            print(f"创建角色: {role.name}")
        else:
            print(f"角色已存在: {role.name}")

    print("基础数据初始化完成!")
    await Tortoise.close_connections()


if __name__ == "__main__":
    asyncio.run(init_auth_data())